#include "Cotton.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <opencv2/imgproc/imgproc.hpp>
#include <librealsense2/rs.hpp>
#include <chrono>


using namespace cv;
using namespace std;

int main() {

    // // ============= detect single image
    // string cfg_file = "../config-cotton.yaml";
    // Mat rgb_image = cv::imread("/home/xag/data/cotton/origindata/2a_118.jpg");


    // Cotton2 Cotton2(cfg_file);
    // auto load_start_pre = std::chrono::high_resolution_clock::now();
    // Cotton2.LoadEngine();
    // auto load_end_pre = std::chrono::high_resolution_clock::now();
    // float load_total_pre = std::chrono::duration<float, std::milli>(load_end_pre - load_start_pre).count();
    // std::cout << "LoadEngine take: " << load_total_pre << " ms." << std::endl;

    // std::map<int, std::vector<int>> object_number;

    // auto in_start_pre = std::chrono::high_resolution_clock::now();
    // Cotton2.InferenceFolder_single2(rgb_image, object_number);
    // auto in_end_pre = std::chrono::high_resolution_clock::now();
    // float in_total_pre = std::chrono::duration<float, std::milli>(in_end_pre - in_start_pre).count();
    // std::cout << "InferenceFolder_single2 take: " << in_total_pre << " ms." << std::endl;

    // cout << "object_number numbers: " << object_number.size() << endl;

    // // for (int i = 0; i < object_number.size(); i++)
    // // {   // circle(img, (x, y), 2, (0, 0, 255), 3)
    // //     cout << "int(object_number[i].h * IMAGE_WIDTH) is " << object_number[i][0] << endl;
    // //     cout << "int(object_number[i].h * IMAGE_WIDTH) is " << object_number[i][1] << endl;
    // //     cout << "int(object_number[i].h * IMAGE_WIDTH) is " << object_number[i][2] << endl;
    // //     cout << "int(object_number[i].h * IMAGE_WIDTH) is " << object_number[i][3] << endl;

    // // }


    // read files image to test
    string cfg_file = "../config-cotton.yaml";
    Cotton2 Cotton2(cfg_file);
    Cotton2.LoadEngine();

    std::vector<cv::String> filenames;
    cv::String folder = "/home/xag/data/cotton/origindata";
	cv::glob(folder, filenames);
 
    cout << "total images are: " << filenames.size() << endl;
    for (size_t j = 0; j < 10; j++)
    {
        for(size_t i = 0; i < filenames.size(); ++i)
        {
            std::map<int, std::vector<int>> object_number;
            
            std::cout<<filenames[i]<<std::endl;
            cv::Mat src = cv::imread(filenames[i]);

            if(!src.data)
                std::cerr << "Problem loading image!!!" << std::endl;

            Cotton2.InferenceFolder_single2(src, object_number);

            // cv::imshow("temp",src);
            // cv::waitKey(0);
            cout << "filenames[i] are: " << filenames[i] << endl;
  
        }
    }

    return 0;

}


// int main() try {

//     // ============= use realsense read RGB data and do inference ========================
//     string cfg_file = "../config-cotton.yaml";
//     Cotton2 Cotton(cfg_file);
//     Cotton.LoadEngine();
//     //声明彩色图
//     rs2::colorizer color_map;
 
//     //声明realsense管道，
//     rs2::pipeline pipe;
//     //数据流配置信息
//     rs2::config pipe_config;
//     pipe_config.enable_stream(RS2_STREAM_DEPTH,640,480,RS2_FORMAT_Z16,30);
//     pipe_config.enable_stream(RS2_STREAM_COLOR,640,480,RS2_FORMAT_BGR8,30);
//     //开始传送数据流
//     rs2::pipeline_profile profile=pipe.start(pipe_config);
//     const char* depth_win="depth_Image";
//     namedWindow(depth_win,WINDOW_AUTOSIZE);
//     const char* color_win="color_Image";
//     namedWindow(color_win,WINDOW_AUTOSIZE);
 
// //    //获取深度像素与长度单位的关系
// //    float depth_scale = get_depth_scale(profile.get_device());
// //    rs2_stream align_to = find_stream_to_align(profile.get_streams());
 
//     while(waitKey(1)&&cvGetWindowHandle(depth_win)){
//         rs2::frameset data=pipe.wait_for_frames();//等待下一帧
 
//         rs2::frame depth=data.get_depth_frame().apply_filter(color_map);//获取深度图，加颜色滤镜
// //        rs2::depth_frame depth=data.get_depth_frame().apply_filter(color_map);//获取深度图，加颜色滤镜
//         rs2::frame color=data.get_color_frame();
 
//         //获取宽高
//         const int depth_w=depth.as<rs2::video_frame>().get_width();
//         const int depth_h=depth.as<rs2::video_frame>().get_height();
//         const int color_w=color.as<rs2::video_frame>().get_width();
//         const int color_h=color.as<rs2::video_frame>().get_height();
 
//         //创建OPENCV类型 并传入数据
// //        Mat depth_image(Size(depth_w,depth_h),CV_16UC1,(void*)depth.get_data(),Mat::AUTO_STEP);
//         Mat depth_image(Size(depth_w,depth_h),CV_8UC3,(void*)depth.get_data(),Mat::AUTO_STEP);
//         Mat color_image(Size(color_w,color_h),CV_8UC3,(void*)color.get_data(),Mat::AUTO_STEP);

//         // 显示
//         std::cout << "depth_w: " << depth_w << " depth_h: " << depth_h << std::endl;
//         std::cout << "color_w: " << color_w << " color_h: " << color_h << std::endl;
//         imshow(depth_win,depth_image);
//         imshow(color_win,color_image);

// //         // start do inference
// //         Cotton.InferenceFolder_single2(color_image);
// // //        imwrite("output.png", color_image);
//     }
//     return EXIT_SUCCESS;

// }

// catch (const rs2::error &e){
//     std::cout<<"RealSense error calling"<<e.get_failed_function()<<"("<<e.get_failed_args()<<"):\n"
//             <<e.what()<<endl;
//     return EXIT_FAILURE;
// }
// catch (const std::exception &e){
//     std::cout<<e.what()<<endl;
//     return EXIT_FAILURE;
// }



